package main

import (
	"fmt"
	"github.com/dgrijalva/jwt-go"
	"time"
)

type Claims struct {
	Username string `json:"username"`
	Role string `json:"role"`
	Portraiturl string `json:"portraiturl"`
	jwt.StandardClaims
}

// 创建用户签名的JWT key
// 关于jwtKey的生成可以使用自己的算法来保证唯一性，这里为了简单就直接用字符串代替了
var jwtKey = []byte("beijingaskme201911061517")

func CreateToken(username string,role string, portraiturl string, expirationTime time.Time) (tokenString string, err error) {
	claims := Claims{
		StandardClaims: jwt.StandardClaims{
			ExpiresAt: expirationTime.Unix(),
		},
	}
	claims.Username = username
	claims.Role = role
	claims.Portraiturl = portraiturl
	// 声明生成token的算法和claims
	token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
	// 创建token字符串
	return token.SignedString(jwtKey)
}

func main()  {
	timestamp := 1513580645
	//注意这个参数暂时用不到
	temppara := time.Unix(int64(timestamp),0)
	token,_ :=  CreateToken("andy","super","url",temppara)
	fmt.Print(token)
}
